import threading

from mztu import Mztu


class SpiderThread(threading.Thread):
    """
    爬虫线程类
    """
    def __init__(self, thread_id, name, url, file_root):
        threading.Thread.__init__(self)
        self.thread_id = thread_id
        self.name = name
        self.url = url
        self.file_root = file_root

    def run(self):
        print("正在启动 %d %s 线程" % (self.thread_id, self.name))
        self.mztu_spider()
        print("正在结束 %d %s 线程" % (self.thread_id, self.name))

    def mztu_spider(self):
        meizitu = Mztu()
        print("------正在寻找找图集------")
        url_list = meizitu.get_picture_url_list(self.url)
        print(url_list)
        index = 1
        for page_url in url_list:
            print("------正在寻找找第" + index.__str__() + "个图集------")
            print("图片地址:" + page_url)
            page_info = meizitu.get_page_info(page_url)
            page_size = page_info[0]
            page_title = page_info[1]
            print("共找到" + page_size + "张图片")
            picture_url = meizitu.get_picture_url(page_url, page_size)
            print("正在下载第" + index.__str__() + "个图集")
            file_path = meizitu.get_file_path(self.file_root, page_size, page_title)
            meizitu.save_picture(picture_url, file_path)
            print("------第" + index.__str__() + "个图集下载完成------")
            index = index + 1
